package main

import (
	"time"

	"gitee.com/ameise84/e3dcs"
	"gitee.com/ameise84/e3dcs/internal/discov"
	"gitee.com/ameise84/e3dcs/internal/log"
	"gitee.com/ameise84/e3dcs/internal/message"
	"gitee.com/ameise84/e3utils/sys"
)

type ServiceAFactory struct {
}

func (s *ServiceAFactory) TakeActor() e3dcs.ActorAdapter {
	return &ServiceA{}
}

func (s *ServiceAFactory) FreeActor(actor e3dcs.ActorAdapter) {

}

func (s *ServiceAFactory) OnBroadcast(request message.Request) {
	//TODO implement me
	panic("implement me")
}

type ServiceA struct {
}

func (ts *ServiceA) Load(cid string, actor e3dcs.Behavior) error {
	log.GetLogger().Trace("Load").Object(actor).Println()
	return nil
}

func (ts *ServiceA) Unload(bool) {
	return
}

func (ts *ServiceA) OnTimer(kind uint16, id uint32, now time.Time, ctx e3dcs.TimerContext) bool {
	return true
}

func (ts *ServiceA) OnRecvMsg(actor e3dcs.Behavior, req message.Request) bool {
	log.GetLogger().Info("OnRecvMsg").Object(actor).Object(req).Println()
	req.ResponseMsg("s2", []byte("ok"))
	actor.Push("s2", []byte("push"))
	return true
}

func main() {
	if _, err := e3dcs.RegisterRpcService("s2", &ServiceAFactory{}, e3dcs.DefaultServiceOptions().SetMode(e3dcs.BLModeHash)); err != nil {
		panic(err)
	}
	err := e3dcs.Start(e3dcs.DefaultDcsOptions())
	if err != nil {
		panic(err)
		return
	}
	sys.WaitKillSigint()
	e3dcs.Stop()
	discov.Stop()
}
